﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Text;
using SharePointClient.SPWebService.Views;
namespace SharePointClient
{
    /// <summary>
    /// View of a List
    /// </summary>
    public class View : WSSBase,IDisposable
    {
        public const string WebService = "_vti_bin/Views.asmx";
        private Guid listID;

        Views views = new Views();

        /// <summary>
        /// Get Sharepoint View of a List
        /// </summary>
        /// <param name="url">Url of List</param>
        /// <param name="listID">List Guid</param>
        /// <param name="viewID">View Guid</param>
        internal View(Connection connection,Uri url, Guid listID, Guid viewID)
            : base(connection,url)
        {
            this.listID = listID;
            Guid = viewID;
        }
        /// <summary>
        /// Get Sharepoint View of a List
        /// </summary>
        /// <param name="url">Url of List</param>
        /// <param name="listID">Guid of List</param>
        /// <param name="partialData">XmlData from View</param>
        internal View(Connection connection,Uri url, Guid listID, XmlNode partialData)
            : base(connection,url)
        {
            this.listID = listID;
            MetaDataElement = partialData;
            Guid = new Guid(SelectItem("/wss:View/@Name"));
            HasNoData = true;
        }

        /// <summary>
        /// Refresh if not refreshed allready
        /// </summary>
        public void Load()
        {
            if (NeedRefresh) Refresh();
        }
        /// <summary>
        /// Returns defined RowLimit of View
        /// </summary>
        public string RowLimit
        {
            get 
            {
                Load(); 
                string s = SelectItem("/wss:View/wss:RowLimit");
                if (s == null) return "100"; else return s;
            }
        }
        
        /// <summary>
        /// Returns ViewFields-XML-Data for Column-Selection in GetListItems()
        /// </summary>
        public XmlNode ViewFields
        {
            get { Load(); return Select("/wss:View/wss:ViewFields")[0]; }
        }
        /// <summary>
        /// Returns a List of Strings with each Column in that view.
        /// </summary>
        public List<string> FieldsInView
        {
            get
            {
                Load();
                XmlNodeList flds = Select("/wss:View/wss:ViewFields/wss:FieldRef");
                List<string> fields = new List<string>();
                for (int i = 0; i < flds.Count; i++) fields.Add(flds[i].Attributes["Name"].Value);
                return fields;
            }
        }
        /// <summary>
        /// Refreshes all View-Information (needed if any User has changed that until load)
        /// </summary>
        public override void Refresh()
        {
            views.Url = Uri.ToString() + WebService;
            views.Credentials = Credentials;
            ProgressEventArgs p=myConnection.DoProgress("Loading View " + Guid.ToString());
            MetaDataElement = views.GetView(listID.ToString(),Guid.ToString());
            NeedRefresh = false;
            p.State = BeforeOrAfter.After;
            p.Operation = "View '" + Title + "' loaded.";
            myConnection.UpdateProgress(p);
        }
        /// <summary>
        /// Return any Attribute from View
        /// </summary>
        /// <param name="pos">Attribute position</param>
        /// <returns>Xml</returns>
        public XmlNode this[int pos]
        {
            get
            {
                Load();
                XmlNodeList fields = Select("//wss:Field");
                if (pos >= 0 && pos < fields.Count) return fields[pos];
                else return MetaData.CreateElement("No Fields");
            }
        }
        public XmlNodeList Fields
        {
            get
            {
                Load();
                return Select("//wss:ViewFields/wss:FieldRef");
            }
        }
        public string Title
        {
            get
            {
                return SelectItem("//wss:View/@DisplayName");
            }
        }
        public bool DefaultView
        {
            get { string n = SelectItem("/wss:View/@DefaultView"); if (n != "") return Convert.ToBoolean(n); else return false; }
        }
        public bool MobileView
        {
            get { Load(); return Convert.ToBoolean(SelectItem("/wss:View/@MobileView")); }
        }
        public override string ToString()
        {
            return Title;
        }
        public Guid ListGuid { get { return listID; } }
        public XmlNode Query
        {
            get { return Select("/wss:View/wss:Query")[0]; }
        }
        #region IDisposable Member

        public void Dispose()
        {
            views.Dispose();
        }

        #endregion
    }


}
